package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.FindDirverView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class FindDirverPresenter extends BasePresenter<FindDirverView> {
    private NjOwnerApiService apiService;

    public FindDirverPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(NjOwnerApiService.class);

    }

    public void getList(int size,int page){
        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("orderId","");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.queryFindDriverOrderList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<DIrverOrderBean>>() {


            @Override
            public void success(BaseOilHttpListResponse<DIrverOrderBean> baseOilHttpListResponse) {
                if(baseOilHttpListResponse!=null&&baseOilHttpListResponse.results!=null){
                    getView().showList(baseOilHttpListResponse.results);
                }

            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);

            }
        });
    }


    public void updateFindDriverOrder(DIrverOrderBean dirverHomeBean){
        Gson gson=new Gson();
        String strEntity = gson.toJson(dirverHomeBean);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        apiService.updateFindDriverOrder(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse baseOilHttpResponse) {
                getView().updateSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }
}
